import * as io from '../services/socketio_Service';
import com from '../utils/common';
import df from '../../config/dataFormat';
import { history } from 'umi';
import {getJSON} from '../utils/request'

export default {
  namespace: 'socketio',

  state: {
    profile_list: [],
    profile_count: 0,
    profile_query: {},
    skip: 1,

    profile: {},
    profile_imgs:[],
    user_list: [],
    rowSelection_id:[],
    rowSelection_rows:[]
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

  effects: {
    *fetch({ payload }, { select }) {
      var x = yield select(({ table: tb }) => tb.table_bd_tags);
    },
    *emit({ payload }, { call }) {
      yield call(io.emit, payload);
    },
    *get_profile_list({}, { put, select }) {
      var state = yield select(({ socketio }) => socketio);
      var { skip, profile_query } = state;
      yield put({
        type: 'emit',
        payload: {
          type: 'get_profile_list',
          data: {
            query: com.set_query(profile_query),
            skip: (skip - 1) * df.pageSize,
            limit: df.pageSize,
          },
        },
      });
    },
    *get_profile({ payload }, { put }) {
      yield put({ type: 'ui/save', payload: { loading: true } });
      yield put({
        type: 'emit',
        payload: {
          type: 'get_profile',
          data: {
            query: payload,
          },
        },
      });
    },
  },

  subscriptions: {
    socketio({ dispatch }) {
      io.listen((action) => {
        switch (action.type) {
          case 'connect':
            com.msg(action.state, action.msg);
            break;
          case 'loading':
            console.log('收到取消Loading状态');
            dispatch({ type: 'ui/save', payload: { loading: action.data } });
            break;
          case 'ok':
            dispatch({ type: 'ui/save', payload: { loading: action.data } });
            com.msg(action.state, action.msg);
            break;
          case 'profile_list':
            var data = action.data.data;
            dispatch({
              type: 'save',
              payload: { profile_list: data, profile_count: action.data.count },
            });
            break;
          case 'profile':
            var data = action.data;
            dispatch({ type: 'ui/save', payload: { loading: false } });
            dispatch({
              type: 'save',
              payload: { profile: data },
            });
            break;
          case 'login':
            dispatch({ type: 'table/save', payload: { user: action.data } });
            history.push('/profile/screen');
            break;
          case 'user_list':
            dispatch({ type: 'save', payload: { user_list: action.data } });
            break;
          // 处理消息提示事件
          default:
            com.msg(action.type, action.msg);
            break;
        }
      });
    },
    init({ dispatch, history }) {

      history.listen(({ pathname, query }) => {
        var patt1 = /(^\/profile\/view\/)(\S*)$/;

        if (pathname === '/profile/screen') {
          dispatch({ type: 'get_profile_list', payload: { skip: 1 } });
        }

        var x = pathname.match(patt1);
        if (x) {
          if (pathname.match(patt1)[1] === '/profile/view/') {
            getJSON(process.env.postUrl + '/api/imgs/' + pathname.match(patt1)[2]).then((imgs)=>{
              dispatch({
                type:'save',
                payload:{profile_imgs:imgs}
              })
            })            
            dispatch({
              type: 'get_profile',
              payload: { _id: pathname.match(patt1)[2] },
            });
          }
        }
        if (pathname === '/system/user') {
          // dispatch({ type:'save', payload: { user_list: true } });
          dispatch({ type: 'emit', payload: { type: 'get_user' } });
        }
      });
    },
  },
};
